package tmi.li.txwidgets.txbtry;


import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/** Helper to the database, manages versions and creation */
public class DBAdapter {
	private static final String TAG ="tmi.li.txwidgets.txbtry.DBAdapter";
	private static final String DATABASE_NAME = "txbtry.db";
	private static final int DATABASE_VERSION = 2;

	// Table name
	public static final String TABLE = "widgets";

	// Columns
	public static final String ID = "_id";
	public static final String FLCOLOR = "FlColor";
	public static final String FTCOLOR = "FtColor";
	public static final String FBCOLOR = "FbColor";
	public static final String BCOLOR = "BColor";
	public static final String PERCENT = "Percent";
	public static final String RTIME = "RestTime";
	public static final String TIME = "Time";
	public static final String PLUG = "Plugged";
	public static final String TEMP = "Temperature";
	public static final String FORMAT = "Format";

	public static final String DATABASE_CREATE = "create table "+TABLE+" (_id integer primary key, "
	+ FLCOLOR+" text," 
	+ FTCOLOR+" text,"
	+ FBCOLOR+" text,"
	+ BCOLOR+" text,"
	+ PERCENT+" text,"
	+ RTIME+" text,"
	+ TIME+" text,"
	+ PLUG+" text,"
	+ TEMP+" text,"
	+ FORMAT+" text"
	+");";

	@SuppressWarnings("unused")
	private Context context;
	private DatabaseHelper DBHelper;
	private SQLiteDatabase db;


	public DBAdapter(Context context) {
		this.context = context;
		DBHelper = new DatabaseHelper(context);
		db = DBHelper.getWritableDatabase();
	}

	public void close() {
		db.close();
	}
	
	public boolean insertWidget(TxBtryWidget widget) 
	{
		if(db.insert(TABLE, null, widget.getValues()) == -1) return false;
		return true;
	}

	public Cursor getWidget(int id) throws SQLException 
	{
		Cursor mCursor =
			db.query(true, TABLE, new String[] {
					ID,
					FLCOLOR, 
					FTCOLOR,
					FBCOLOR,
					BCOLOR,
					PERCENT,
					RTIME,
					TIME,
					PLUG,
					TEMP,
					FORMAT
			}, 
			ID + "=" + id, 
			null,
			null, 
			null, 
			null, 
			null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	//---updates a title---
	public boolean updateWidget(TxBtryWidget widget) 
	{
		return db.update(TABLE, widget.getValues(), 
				ID + "=" + widget._id, null) > 0;
	}

	public boolean deleteWidget(TxBtryWidget widget) {
		return db.delete(TABLE, ID + "=" + widget._id, null) > 0;
	}

	private static class DatabaseHelper extends SQLiteOpenHelper 
	{
		DatabaseHelper(Context context) 
		{
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) 
		{
			try {
				db.execSQL(DATABASE_CREATE);
			}catch(SQLException e) {

			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, 
				int newVersion) 
		{
			Log.w(TAG, "Upgrading database from version " + oldVersion 
					+ " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS "+TABLE);
			onCreate(db);
		}
	}    

}

